'''
Created on 05/04/2010

@author: User
'''
from Models import DTO
from Models import Playlist, SongVSplaylist
from SongUtils import SongUtils
from google.appengine.api import users



class PlaylistUtils:
    
    def createNewPlaylist(self, name): #return Playlist key string
        newplaylist = Playlist()
        newplaylist.name = name
        newplaylist.isDefault = False        
        newplaylist.user = users.get_current_user()
        newplaylist.put()
        return newplaylist
       
    
    def deletePlaylist(self,keystring):
        pl = Playlist.get(keystring)
        sVSpArr = SongVSplaylist.gql("WHERE playlist_id = :1", keystring).fetch(100000)
        for sVSp in sVSpArr:
            sVSp.delete()
        pl.delete()
        return 1
    
    def renamePlaylist(self, keystring, newname):
        playlist = Playlist.get(keystring)
        playlist.name = newname
        playlist.put()
        return 1
        
         
    def retrievePlaylistList(self):
        user_playlists = Playlist.gql("WHERE user = :1", users.get_current_user()).fetch(1000)
        playlists_dto =[]
        for playlist in user_playlists:
            playlists_dto.append(self.to_dto(playlist))
        return playlists_dto
    
    def importPlaylist(self, playlistName, songsArray):
        songUtilsInst = SongUtils()
        playlist = self.createNewPlaylist(playlistName)
        songUtilsInst.addSongs(str(playlist.key()), songsArray)
        return 1
         
    def to_dto(self, playlist):
        if playlist is None: return None
        dto = DTO()
        dto.keyString = str(playlist.key())
        dto.playlistName = playlist.name
        dto.isDefault = playlist.isDefault
        return dto
    
    
        
        
        
        
        
        